<?php

declare(strict_types=1);

namespace App\Controller\Admin;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use App\Middleware\AdminMiddleware;
use App\Model\BannerImage as ModelBannerImage;

/**
 * @AutoController(prefix="/admin/bannerImage")
 * @Middlewares({
 *     @Middleware(AdminMiddleware::class)
 * })
 */
class BannerImage extends AbstractController
{

    public function info() {
        $this->validate([
            'id' => 'required'
        ]);
        $model = ModelBannerImage::find($this->request->input('id'));
        return $this->success($model);
    }

    public function list() {
        $data = $this->request->all();
        $query = ModelBannerImage::query();

        if (!empty($data['banner_id'])) {
            $query = $query->where('banner_id', $data['banner_id']);
        }
        if (!empty($data['status'])) {
            $query = $query->where('status', $data['status']);
        }

        $query = $query->with('banner')
            ->orderBy('sorting', 'desc')
            ->orderBy('id', 'desc');
        if (isset($data['page_size']) && $data['page_size'] <= 0) {
            $list = $query->get();
        }else{
            $list = $query->paginate($data['page_size'] ?? 20);
        }
        return $this->success($list);
    }

    public function save() {
        $this->validate([
            'banner_id' => 'required',
            'image' => 'required'
        ]);
        $data = $this->request->all();
        $model = ModelBannerImage::upsert($data);
        return $this->success($model);
    }

    public function deleteMany() {
        $this->validate([
            'ids' => 'required|array'
        ]);
        $ids = $this->request->input('ids');
        
        $res = ModelBannerImage::destroy($ids);
        return $this->success($res);
    }
}
